package com.carsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.carsystem.entity.Car;
import com.carsystem.entity.Shop;
import com.carsystem.mapper.CarMapper;
import com.carsystem.mapper.ShopMapper;
import com.carsystem.request.CarFrontRentRequestVo;
import com.carsystem.response.CarFrontRentResponseVo;
import com.carsystem.response.OrderResponseVo;
import com.carsystem.service.ICarService;
import com.carsystem.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ykl
 * @since 2021-12-04
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements ICarService {

    @Resource
    private CarMapper carMapper;
    @Resource
    private ShopMapper shopMapper;

    @Override
    public Result findAllCar() {
        List<Car> cars=null;
        try {
            cars = carMapper.selectList(null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CARBRAND_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_CARBRAND_SUCCESS,cars);
    }

    @Override
    public PageResult findList(QueryPageBean queryPageBean) {
        Page<Car> carPage = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(queryPageBean.getQueryString())){
            queryWrapper.like("car_name",queryPageBean.getQueryString())
                    .or().like("plateNumber",queryPageBean.getQueryString());
        }
        Page<Car> selectPage = carMapper.selectPage(carPage, queryWrapper);

        return new PageResult(selectPage.getTotal(),selectPage.getRecords());

    }

    @Override
    public Result addCar(Car car) {
        int insert = carMapper.insert(car);
        if (insert==1){
            return new Result(true,MessageConstant.ADD_CAR_SUCCESS);
        }
        return new Result(false,MessageConstant.ADD_CAR_FAIL);
    }

    @Override
    public Result updateCar(Car car) {
        int i = carMapper.updateById(car);
        if (i==1){
            return new Result(true,MessageConstant.EDIT_CAR_SUCCESS);
        }
        return new Result(false,MessageConstant.EDIT_CAR_FAIL);
    }

    @Override
    public Result deleteCar(int carId) {
        int i = carMapper.deleteById(carId);
        if (i==1){
            return new Result(true,MessageConstant.DELETE_CAR_SUCCESS);
        }
        return new Result(false,MessageConstant.DELETE_CAR_FAIL);
    }

    @Override
    public PageHandler selectAllCar(CarFrontRentRequestVo carFrontRentRequestVo) {
        Map<String, Object> map = new HashMap<>();
        Integer offset = (carFrontRentRequestVo.getPageIndex() - 1) * carFrontRentRequestVo.getPageSize();
        map.put("offset",offset);
        map.put("pageSize", carFrontRentRequestVo.getPageSize());
        if (StringUtils.isNotBlank(carFrontRentRequestVo.getType())){
            map.put("type",carFrontRentRequestVo.getType());
        }
        if (StringUtils.isNotBlank(carFrontRentRequestVo.getCarBrand())){
            map.put("carBrand",carFrontRentRequestVo.getCarBrand());
        }
        if (StringUtils.isNotBlank(carFrontRentRequestVo.getCarName())){
            map.put("carName",carFrontRentRequestVo.getCarName());
        }

        Integer count = carMapper.selectAllCarCount(map);
        List<CarFrontRentResponseVo> carFrontRentResponseVos = carMapper.selectAllCar(map);
        PageHandler pageHandler = new PageHandler(carFrontRentRequestVo.getPageIndex(), carFrontRentRequestVo.getPageSize(),count);
        pageHandler.setList(carFrontRentResponseVos);
        return pageHandler;
    }
}
